package com.example.canopytemperature;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

//大棚节点列表的适配器
public class NodeListAdapter extends BaseAdapter {
    Context context;
    ArrayList airTemList;
    ArrayList soilTemList;
    ArrayList illuminationList;
    ArrayList airHumList;
    ArrayList soilHumList;
    ArrayList titleList;
    ArrayList id;
    LayoutInflater inflater;


    public NodeListAdapter(Context context, ArrayList airTemList, ArrayList soilTemList, ArrayList illuminationList, ArrayList airHumList, ArrayList soilHumList, ArrayList titleList, ArrayList id) {
        this.context = context;
        this.airTemList = airTemList;
        this.soilTemList = soilTemList;
        this.illuminationList = illuminationList;
        this.airHumList = airHumList;
        this.soilHumList = soilHumList;
        this.titleList = titleList;
        this.id = id;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return airTemList.size();
    }

    @Override
    public Object getItem(int i) {

        return airTemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.node_list, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.node_airHum.setText(airHumList.get(i).toString());
        viewHolder.node_airTem.setText(airTemList.get(i).toString());
        viewHolder.node_illumination.setText(illuminationList.get(i).toString());
        viewHolder.node_soilHum.setText(soilHumList.get(i).toString());
        viewHolder.node_soilTem.setText(soilTemList.get(i).toString());
        viewHolder.node_title.setText(titleList.get(i).toString());
        setIconShow(i, viewHolder);
        return view;
    }

    private void setIconShow(int i, ViewHolder viewHolder) {
        float airHumMinFloat = Utils.getFloat(context, id.get(i).toString(), "airHumMinFloat");
        float airHumMaxFloat = Utils.getFloat(context, id.get(i).toString() + "", "airHumMaxFloat");

        if (!(airHumMinFloat == 0.0 || airHumMaxFloat == 0.0)) {
            if (airHumMinFloat >= Float.parseFloat(airHumList.get(i).toString())) {
                viewHolder.node_airHumimg.setImageResource(R.drawable.icon);
            } else if (airHumMaxFloat <= Float.parseFloat(airHumList.get(i).toString())) {
                viewHolder.node_airHumimg.setImageResource(R.drawable.icon);
            } else {
                viewHolder.node_airHumimg.setImageResource(R.drawable.noicon);
            }
        } else {
            viewHolder.node_airHumimg.setImageResource(R.drawable.noicon);
        }
        float airTemMinFloat = Utils.getFloat(context, id.get(i).toString() + "", "airTemMinFloat");
        float airTemMaxFloat = Utils.getFloat(context, id.get(i).toString() + "", "airTemMaxFloat");
        if (!(airTemMinFloat == 0.0 || airTemMaxFloat == 0.0)) {
            if (airTemMinFloat >= Float.parseFloat(airTemList.get(i).toString())) {
                viewHolder.node_airTemimg.setImageResource(R.drawable.icon);
            } else if (airTemMaxFloat <= Float.parseFloat(airTemList.get(i).toString())) {

                viewHolder.node_airTemimg.setImageResource(R.drawable.icon);
            } else {
                viewHolder.node_airTemimg.setImageResource(R.drawable.noicon);
            }
        } else {
            viewHolder.node_airTemimg.setImageResource(R.drawable.noicon);
        }
        float soilHumMinFloat = Utils.getFloat(context, id.get(i).toString() + "", "soilHumMinFloat");
        float soilHumMaxFloat = Utils.getFloat(context, id.get(i).toString() + "", "soilHumMaxFloat");
        if (!(soilHumMinFloat == 0.0 || soilHumMaxFloat == 0.0)) {
            if (soilHumMinFloat >= Float.parseFloat(soilHumList.get(i).toString())) {
                viewHolder.node_soilHumimg.setImageResource(R.drawable.icon);
            } else if (soilHumMaxFloat <= Float.parseFloat(soilHumList.get(i).toString())) {
                viewHolder.node_soilHumimg.setImageResource(R.drawable.icon);
            } else {
                viewHolder.node_soilHumimg.setImageResource(R.drawable.noicon);
            }
        } else {
            viewHolder.node_soilHumimg.setImageResource(R.drawable.noicon);
        }
        float soilTemMinFloat = Utils.getFloat(context, id.get(i).toString() + "", "soilTemMinFloat");
        float soilTemMaxFloat = Utils.getFloat(context, id.get(i).toString() + "", "soilTemMaxFloat");
        if (!(soilTemMinFloat == 0.0 || soilTemMaxFloat == 0.0)) {
            if (soilTemMinFloat >= Float.parseFloat(soilTemList.get(i).toString())) {
                viewHolder.node_soilTemimg.setImageResource(R.drawable.icon);
            } else if (soilTemMaxFloat <= Float.parseFloat(soilTemList.get(i).toString())) {
                viewHolder.node_soilTemimg.setImageResource(R.drawable.icon);
            } else {
                viewHolder.node_soilTemimg.setImageResource(R.drawable.noicon);
            }
        } else {
            viewHolder.node_soilTemimg.setImageResource(R.drawable.noicon);
        }
        float illuminaionMinFloat = Utils.getFloat(context, id.get(i).toString() + "", "illuminaionMinFloat");
        float illuminaionMaxFloat = Utils.getFloat(context, id.get(i).toString(), "illuminaionMaxFloat");
        if (!(illuminaionMinFloat == 0.0 || illuminaionMaxFloat == 0.0)) {
            if (illuminaionMinFloat >= Float.parseFloat(illuminationList.get(i).toString())) {
                viewHolder.node_illuminationimg.setImageResource(R.drawable.icon);
            } else if (illuminaionMaxFloat <= Float.parseFloat(illuminationList.get(i).toString())) {
                viewHolder.node_illuminationimg.setImageResource(R.drawable.icon);
            } else {
                viewHolder.node_illuminationimg.setImageResource(R.drawable.noicon);
            }
        } else {
            viewHolder.node_illuminationimg.setImageResource(R.drawable.noicon);
        }
    }

    class ViewHolder {
        TextView node_airTem, node_airHum, node_illumination, node_soilHum, node_soilTem, node_title;
        ImageView node_airTemimg, node_airHumimg, node_illuminationimg, node_soilHumimg, node_soilTemimg;

        public ViewHolder(View view) {
            node_airTem = view.findViewById(R.id.node_airTem);
            node_airHum = view.findViewById(R.id.node_airHum);
            node_illumination = view.findViewById(R.id.node_illumination);
            node_soilHum = view.findViewById(R.id.node_soilHum);
            node_soilTem = view.findViewById(R.id.node_soilTem);
            node_title = view.findViewById(R.id.node_title);
            node_airTemimg = view.findViewById(R.id.node_airTemimg);
            node_airHumimg = view.findViewById(R.id.node_airHumimg);
            node_illuminationimg = view.findViewById(R.id.node_illuminationimg);
            node_soilHumimg = view.findViewById(R.id.node_soilHumimg);
            node_soilTemimg = view.findViewById(R.id.node_soilTemimg);
        }
    }
}
